<?php
/**
 * Created by PhpStorm.
 * User: Office
 * Date: 2017/12/21
 * Time: 17:04
 */

header("Content-type: text/html; charset=utf-8");
session_start();
$ok = false;
if (!$_POST['entry']) {
    exit("参数错误！");
}
if (empty($_SESSION['user']) || $_SESSION['user'] !== 'admin') {
    echo '请<a href="../index.php" style="color: red">登录</a>后执行操作';
    exit;
}

$filename = $_POST['entry'];
if (file_exists($filename)) {
    $fp = @fopen($filename, "r");
    if ($fp) {
        flock($fp, LOCK_SH);
        $result = fread($fp, filesize($filename));
    }
    flock($fp, LOCK_UN);
    fclose($fp);
    $content_arr = explode('|', $result);
}

if (isset($_POST['title']) && isset($_POST['content'])) {
    $title = trim($_POST['title']);
    $content = trim($_POST['content']);

    $new_tit = str_replace($content_arr[0], $title, $result);
    $new_con = str_replace($content_arr[2], $content, $new_tit);

    $fp = @fopen($filename, 'w');
    if ($fp) {
        flock($fp, LOCK_EX);
        $result = fwrite($fp, $new_con);
        flock($fp, LOCK_UN);
        fclose($fp);
    }
    if (strlen($result) > 0) {
        $ok = true;
        $msg = '日志修改成功,<a href="blog_in.php?entry=' . $_POST['entry'] . '" style="color: orangered">查看该日志文章</a>';
    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑成功</title>
    <link rel="stylesheet" href="../css/blog.css">

</head>
<body>

<div class="box">
    <header>
        <h2>我的BLOG</h2>
    </header>
    <div class="content clearBox">

        <div class="left">
            <h5 class="title">编辑日志</h5>
            <?php
            if ($ok) {?>
                <div style="padding-top: 10px"></div>
                <?php echo $msg;
            } else {
                ?>
                <form action="edit1.php" method="post" name="edform">
                    <dl>
                        <dt>日志标题：</dt>
                        <dd><input type="text" name="title"/></dd>
                    </dl>

                    <dl>
                        <dt>日志内容：</dt>
                        <dd><textarea name="content"></textarea></dd>
                    </dl>
                    <dl>
                        <dd>最后编辑于：<?php echo @date('Y-m-d H:i:s'); ?> </dd>
                    </dl>
                    <button type="submit" name="btn">提交</button>
                </form>
            <?php } ?>
        </div>
        <div class="right">
            <div>
                <h5 class="title">关于我</h5>
                <p class="right-ctn">php初学者</p>
            </div>
        </div>
    </div>
    <div class="footer">CopyRight&nbsp;2017</div>
    <script>
        var form = document.forms["edform"],
            title = form["title"],
            content = form["content"],
            btn = form["btn"];
        btn.onclick = function () {
            if (title.value === "" || content.value === "") {
                alert("请输入日志标题或内容");
                return false;
            }
        }
    </script>
</body>
</html>
